package com.study.bootstudy.serviice;

import cn.hutool.core.util.ObjectUtil;
import com.study.bootstudy.entity.ServiceOrder;
import com.study.bootstudy.util.ApiResult;
import com.study.bootstudy.util.AssertUtil;
import com.study.bootstudy.util.ErrorCodeEnum;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * @Author RenPu
 * @Date 2022/6/27 17:11
 * @Version 1.0
 * @Description:  codeReview的版本比较
 **/

@Service
public class ApiServiceImpl implements ApiService {

    /******** 原始代码的写法   codeReview之前。。。。。。。。。。 ***********/
    public ApiResult cancelService(@PathVariable Long serviceOrderId){
        // ServiceOrder serviceOrder = serviceOrderMapper.selectByPrimaryKey(serviceOrderId);  伪代码编写
        ServiceOrder serviceOrder=new ServiceOrder();
        Integer cancelOrderStatus=1;
        ApiResult result = new ApiResult<>();
        if (ObjectUtil.isNull(serviceOrder)) {
            result.setIsSuccess(false);
            result.setResponseCode(ErrorCodeEnum.FAIL.getCode());
            result.setResponseMsg("查无此服务单");
            return result;
        }
        if(serviceOrder.getOrderStatus().equals(cancelOrderStatus)){
            result.setIsSuccess(false);
            result.setResponseCode(ErrorCodeEnum.FAIL.getCode());
            result.setResponseMsg("已取消的服务单不允许再次取消");
            return result;
        }
        if(serviceOrder.getSortOrderId() != null){
            result.setIsSuccess(false);
            result.setResponseCode(ErrorCodeEnum.FAIL.getCode());
            result.setResponseMsg("已配置物料的服务单不允许取消");
            return result;
        }
        // ...other check

        // ...do something
        return result;
    }




    /******  代码优化的  codeReview 1.0 version       *******/
   public ApiResult cancelServiceCodeViewOnce(@PathVariable Long serviceOrderId){
       // ServiceOrder serviceOrder = serviceOrderMapper.selectByPrimaryKey(serviceOrderId);  伪代码编写
       ServiceOrder serviceOrder=new ServiceOrder();
       Integer cancelOrderStatus=1;
      if(ObjectUtil.isEmpty(serviceOrder)){
          ApiResult<String> fail = ApiResult.fail(ErrorCodeEnum.FAIL.getCode(),"查无此服务单");
          return fail;
      }
       if(serviceOrder.getOrderStatus().equals(cancelOrderStatus)){
           ApiResult<String> fail = ApiResult.fail(ErrorCodeEnum.FAIL.getCode(),"已取消的服务单不允许再次取消");
           return fail;
       }
       if(serviceOrder.getSortOrderId() != null){
           ApiResult<String> fail = ApiResult.fail(ErrorCodeEnum.FAIL.getCode(),"已配置物料的服务单不允许取消");
           return fail;
       }
        return new ApiResult();
   }





    /******  代码优化的  codeReview 2.0 version        *******/
    public ApiResult cancelServiceCodeViewTwice(@PathVariable Long serviceOrderId){
        // ServiceOrder serviceOrder = serviceOrderMapper.selectByPrimaryKey(serviceOrderId);  伪代码编写
        ServiceOrder serviceOrder=new ServiceOrder();
        Integer cancelOrderStatus=1;
        if(ObjectUtil.isEmpty(serviceOrder)){
            ApiResult<String> fail = ApiResult.fail(ErrorCodeEnum.FAIL.getCode(),"查无此服务单");
            return fail;
        }
        if(serviceOrder.getOrderStatus().equals(cancelOrderStatus)){
            ApiResult<String> fail = ApiResult.fail(ErrorCodeEnum.FAIL.getCode(),"已取消的服务单不允许再次取消");
            return fail;
        }
        if(serviceOrder.getSortOrderId() != null){
            ApiResult<String> fail = ApiResult.fail(ErrorCodeEnum.FAIL.getCode(),"已配置物料的服务单不允许取消");
            return fail;
        }
        return new ApiResult();
    }


     //异常处理类+断言处理得更加简化。
    /******  代码优化的  codeReview 3.0 version        *******/
    @Override
   public ApiResult cancelServiceCodeViewThree(int serviceOrderId){
       //ServiceOrder serviceOrder = serviceOrderMapper.selectByPrimaryKey(serviceOrderId);
       ServiceOrder serviceOrder=new ServiceOrder ();
       serviceOrder.setOrderStatus(1);
       int cancelOrderStatus=2;
       AssertUtil.businessInvalid(ObjectUtil.isNull(serviceOrder),"查无此服务单");
       AssertUtil.businessInvalid(serviceOrder.getOrderStatus().equals(cancelOrderStatus),"查无此服务单");
       AssertUtil.businessInvalid(serviceOrder.getSortOrderId() != null,"查无此服务单");
        //...other check

       // ...do something
       return ApiResult.success();
   }





}
